package jpave.query_interface;

import jpave.dataholders.GlobalSettings;
import util.Debug.ErrorReport;
import util.methods.CommandLineParser;
import util.methods.Version;

/* 
 *  Main class for the viewSingleTCW (used to be called Pave)
 *  brings up the listing of all assemblies from the HOSTS.cfg
 */

public class JPaveMain
{	
	public static final String JPAVE_VERSION = "v" + Version.strPAVEver; 
	public static final String JPAVE_VERSION_STRING = "viewSingleTCW " + 
		JPAVE_VERSION + " " + Version.strRelDate +" - " +  Version.URL; 

	public static boolean debug = false;
	public static boolean updateMSG = false; // update Overview, read by OverviewDBWrapper

	public static void main(String[] args)
	{   
		//System.err.println("Starting with: \n" + Runtime.getRuntime().freeMemory() + " bytes free");
		//System.err.println((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) + " bytes used");
		System.err.println("\n" + JPaveMain.JPAVE_VERSION_STRING);
		
		if (!Version.checkJavaSupported())
			System.exit(-1);
		
		if (CommandLineParser.hasOption(args, "-h") 
				|| CommandLineParser.hasOption(args, "-help") 
				|| CommandLineParser.hasOption(args, "help")) 
		{
			System.err.println(
					  JPAVE_VERSION_STRING + "\n"
					+ "Usage:  viewSingleTCW [singleTCW ID] [-s]\n" 
					+ "        The optional singleTCW ID must be the ID, not database name."
					+ "        The optional -s forces the overview to be regenerated."
					+ "    -h|-help|help   Display this help screen.");
			return;
		}
		
		if (CommandLineParser.hasOption(args, "-s")) {// obsolete for now
			System.err.println("Update Overview");
			updateMSG = true;
		}
		// Load the list of assemblies from the config file
		 GlobalSettings settings = loadSettings();
		 if (settings == null) 
		 {
			 System.err.println("No assemblies to view!");
			 return;
		 }
		    
		// Show the main frame dialog

		JPaveFrame.setupUIDefaults();
		JPaveFrame frame = null;
		if (args.length > 0) {
			String db = args[0];
			frame = new JPaveFrame ( settings, db ); 
		}
		else
			frame = new JPaveFrame ( settings ); 	
		frame.setVisible( true );
		
		return;
	}
    
    static GlobalSettings loadSettings ()
    {

        try
        {
            // Load in the assemblies from the configuration file
        		GlobalSettings settings = new GlobalSettings ();
        		settings.loadFromHosts( ); 
            return settings;
        }
        catch ( Exception err )
        {
            ErrorReport.reportError(err, "Error: loading the configuration file.");
            return null;
       }
    }
}